//
//  ZDEditPasswordController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/20.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDEditPasswordController.h"
#import "ZDInputFieldCell.h"
#import "ZDResignCell.h"
#import "NSString+MD5.h"
#import "ZDLoginController.h"

static NSString *const ZDInputFieldCellId = @"ZDInputFieldCell";
static NSString *const ZDResignCellId = @"ZDResignCell";

@interface ZDEditPasswordController ()
/**
 <#Description#>
 */
@property(nonatomic, strong) NSArray *titleArray;
/**
 <#Description#>
 */
@property(nonatomic, strong) NSArray *placeArray;
/**
 <#Description#>
 */
@property(nonatomic, copy) NSString *orginPassword;
/**
 new
 */
@property(nonatomic, copy) NSString *password;

@end

@implementation ZDEditPasswordController

- (void)setupUI
{
    [super setupUI];
    self.title = @"修改密码";
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDInputFieldCell" bundle:nil] forCellReuseIdentifier:ZDInputFieldCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDResignCell" bundle:nil] forCellReuseIdentifier:ZDResignCellId];
    self.tableView.rowHeight = 55;
}
- (void)loadData
{
    self.titleArray = @[@"原始密码:",@"新密码:",@"确认密码:"];
    self.placeArray = @[@"原始密码",@"新密码",@"新密码"];
    [self.tableView reloadData];
}
#pragma mark- 网络请求
- (void)requstSavePassword
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[self.orginPassword stringToMD5] forKey:@"password"];
    [dict setObject:[self.password stringToMD5] forKey:@"new_password"];
 
    
    [SVProgressHUD showWithStatus:@"修改中"];
    __weak typeof (self) weakSelf = self;
    [API requestPostAppendKey:EditPasswordUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                ZDLoginController *login = [[ZDLoginController alloc] init];
                [weakSelf presentViewController:login animated:YES completion:nil];
            });
        }

    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3) {
        ZDResignCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDResignCellId forIndexPath:indexPath];
        [cell.resignButton setTitle:@"提交修改" forState:0];
        cell.resignButton.enabled = YES;
        [cell.resignButton setBackgroundColor:LEHaxColor(0xff6606)];
        [cell.resignButton addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    ZDInputFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDInputFieldCellId forIndexPath:indexPath];
    cell.titleLabel.text = self.titleArray[indexPath.row];
    cell.inputField.placeholder = self.placeArray[indexPath.row];
    return cell;
}

#pragma mark- 点击事件
- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)saveClick
{
    self.orginPassword = [self getFieldText:0];
    self.password = [self getFieldText:1];
    if (![self.password isEqualToString:[self getFieldText:2]]) {
        [SVProgressHUD showInfoWithStatus:@"两次输入密码不一致"];
        return;
    }
    if (self.orginPassword.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入原始密码"];
        return;
    }
    
    [self requstSavePassword];
}
/**
 根据行号获取输入框的内容
 
 @param indexPathRow <#indexPathRow description#>
 @return <#return value description#>
 */
- (NSString *)getFieldText:(NSInteger)indexPathRow
{
    NSIndexPath *path = [NSIndexPath indexPathForRow:indexPathRow inSection:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
    if ([cell isKindOfClass:[ZDInputFieldCell class]])
    {
        ZDInputFieldCell *cells = (ZDInputFieldCell *)cell;
        return  cells.inputField.text;
    }
    return @"";
}
@end






